package com.pzhu.acargid.recycled;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.pzhu.acargid.doc.DocService;
import com.pzhu.acargid.domain.Doc;
import com.pzhu.acargid.domain.Recycled;
import com.pzhu.acargid.model.ModelService;
import com.pzhu.acargid.until.Result;
import com.pzhu.security.token.JwtTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/recycle")
public class RecycledController {

    @Autowired
    private RecycledService recycledService;
    @Autowired
    private DocService docService;
    @Autowired
    private ModelService modelService;

    @PostMapping("/expunge")
    public Result expunge(Integer id) {
//        Integer userid = JwtTokenService.getClaim(request, "userid").asInt();
//        LambdaUpdateWrapper<Recycled> recycledLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//        recycledLambdaUpdateWrapper.eq(Recycled::getId, id);
//        recycledLambdaUpdateWrapper.eq(Recycled::getUserId, userid);
//        recycledLambdaUpdateWrapper.set(Recycled::getRemoved, 1);
        if (recycledService.removeById(id))
            return new Result(1, null, "操作成功");
        return new Result(0, null, "操作失败");
    }

    @PostMapping("/restore")
    public Result restore(HttpServletRequest request, Integer id) {
        Integer userid = JwtTokenService.getClaim(request, "userid").asInt();
        Recycled byId = recycledService.getById(id);
        boolean restore = docService.restore(userid, byId.getDocId());
        if (restore) {
            if (recycledService.removeById(id))
                return new Result(1, null, "操作成功");
        }
        return new Result(0, null, "操作失败");
    }

}
